import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.google.common.collect.Queues;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by donar on 17/3/20.
 */
public class FetchAMZProductMetaTask implements Runnable{

    CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
    LinkedBlockingQueue<String> tasks;
    public FetchAMZProductMetaTask(LinkedBlockingQueue queue){
        this.tasks=queue;
    }
    public  String fetch(String type)  {
        try{
            if(Strings.isNullOrEmpty(type))return null;
            String url ="https://catalog-sc.amazon.ca/abis/Classify/SelectCategory?productType="+type;
            System.out.println(url);
            HttpGet httpGet = new HttpGet(url);
            addHeader(httpGet);
            HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
            String content = EntityUtils.toString(httpResponse.getEntity());
            String result = content.substring(content.indexOf("viewModel:")+10);
            result=result.substring(0,result.indexOf(",\n" +
                    "\t\tfeatures: {fees: false},"));
            System.out.println(result);
            JSONObject o = JSONObject.parseObject(result);
            JSONArray jsonArray = o.getJSONObject("metadata").getJSONArray("menus");
            return JSON.toJSONString(jsonArray);
        }catch (Exception e){
            System.out.println("error " + type);
            e.printStackTrace();
        }
        return null;
    }

    public static void addHeader(HttpGet httpPost){
        httpPost.addHeader("Cookie","JSESSIONID=0C16ABEE6927A595E05732A5EC87BDF6; s_pers=%20s_ev15%3D%255B%255B%2527Typed/Bookmarked%2527%252C%25271489038341892%2527%255D%252C%255B%2527Typed/Bookmarked%2527%252C%25271489038343320%2527%255D%252C%255B%2527Typed/Bookmarked%2527%252C%25271489038343469%2527%255D%252C%255B%2527Typed/Bookmarked%2527%252C%25271489038345512%2527%255D%252C%255B%2527Typed/Bookmarked%2527%252C%25271489038346207%2527%255D%252C%255B%2527Typed/Bookmarked%2527%252C%25271489038346355%2527%255D%252C%255B%2527Typed/Bookmarked%2527%252C%25271489038349059%2527%255D%255D%7C1646804749059%3B%20s_dl%3D1%7C1490016055001%3B%20gpv_page%3DUS%253ASC%253A%2520SellerCentralLogin%7C1490016055006%3B; at-acbca=Atza|IwEBIJvLpo4XEfidCsdHrVGE4vIV_2cVH1kmpqbWWeqMRJtfNotSkIS8d3Xl9pUgHMK_pP0FF4doqwEvWa3E1Fd2R5v8arOrb6YFRXmZ7yviojALMqXELxyTr12x9hp9RxG4-cliGVLGJmp8Kp9vETnUs8YF1KfD8a_l3ab9Xi5avm5d1YZzWf_gvYR5apRWEJrg1ohmeSv8cfU6gXRDk-6qvpVu8Xi9nGwZTR2Cmn0ekmpxfxcQ6wEGmFWnAK7-JDHmoHuiXbSCjJxf-Wij8OrPGtKgH3sRZ_poh8xovfuXaDps2C2hpWh_NmZ3w2WfRLYvgOpea7RSNo_LvoXyJnZLuJAMxQr7BmWm0SrVUxqden1z8JioiTSOouvfQeTkeQHzuYUBFNlnLRWJHjIlce4mAArB; sess-at-acbca=\"c0ggYb8t3dyKmgFidpWG415qF17aSpO4EB1Zrzc9KbY=\"; session-id-time=1490598000l; session-id=152-2092848-7709143; x-acbca=\"OBoUc1PFEMoJUBV0DX67qp@L@RQy9G@L??QldXsGP9@6M3mjd5uzga3FzXLv7hFZ\"; s_sess=%20c_m%3DundefinedTyped/BookmarkedTyped/Bookmarked%3B%20s_cc%3Dtrue%3B%20ev1%3Dn/a%3B%20s_sq%3Damznsrvsprod%252Camznsrvsmainprod%253D%252526pid%25253DUS%2525253ASC%2525253A%25252520SellerCentralLogin%252526pidt%25253D1%252526oid%25253Dhttps%2525253A//images-na.ssl-images-amazon.com/images/G/15/rainier/registration/sign-in-md-pri._CB374805626%252526ot%25253DIMAGE%3B; ubid-acbca=168-1404245-6526969");
        httpPost.addHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
        httpPost.addHeader("Accept","*/*");
        //httpPost.addHeader("Referer","https://sellercentral.amazon.com/hz/productclassify?query=&ref_=xx_pclasshm_cont_prodsrch&");
        httpPost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6");
        httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
        //httpPost.addHeader("X-Requested-With","XMLHttpRequest");

    }

    public void run() {
        String task =null;
        try {

            while((task=tasks.take())!=null){
                String[] a = task.split(";");
                String result = fetch(a[0]);
                Connection connection = JdbcUtil.getConnection();
                PreparedStatement pstmt = connection.prepareStatement("insert into type_meta_info (type,site,meta_info) VALUE (?,?,?)");
                pstmt.setString((int)1,a[0]);
                pstmt.setString((int)2,a[1]);
                pstmt.setString((int)3,result);
                pstmt.execute();
                pstmt.close();
                JdbcUtil.releaseConn(connection);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws SQLException {
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 20; i++) {
            FetchAMZProductMetaTask task = new FetchAMZProductMetaTask(queue);
            executorService.execute(task);
        }
        Connection connection = JdbcUtil.getConnection();
        ResultSet rs = connection.createStatement().executeQuery("select distinct productTypeDefinitions,site from amazon_catagory where productTypeDefinitions!='' and site='CANADA'");
        while(rs.next()){
            String[] arrs=rs.getString("productTypeDefinitions").split(",");
            for (String a :arrs){
                queue.offer(a+";"+rs.getString("site"));
            }
        }
        rs.close();
        JdbcUtil.releaseConn(connection);
    }
}
